<template>
    <div class="login-wrapper">
        <div class="login-content">
            <div class="titleHead">商家后台</div>
            <div class="title">
                <div class="text">
                    {{ flag == '0' ? '用户登录' : '用户注册' }}
                </div>
                <div class="go-register" @click="hanldeFlag">
                    {{ flag == '0' ? '暂未账号' : '已有账号' }}，
                    <span> {{ flag == '0' ? '直接注册' : '直接登录' }}</span>
                </div>
            </div>
            <div class="content">
                <el-form :model="loginInfo">
                    <el-form-item prop="account">
                        <el-input v-model="loginInfo.account" placeholder="请输入账号" class="account">
                            <i slot="prefix" class="el-input__icon el-icon-user"></i>
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input v-model="loginInfo.password" type="password" placeholder="请输入密码" class="password">
                            <i slot="prefix" class="el-input__icon el-icon-goods"></i>
                        </el-input>
                    </el-form-item>
                    <div @click="handleLogin" v-if="flag == '0'" class="loginBtn">登录</div>
                    <div @click="handleRegister" v-if="flag == '1'" class="loginBtn">注册</div>
                    <!-- <el-button @click="handelC">C端</el-button> -->
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
import { register, login } from "@/api/login"
import utils from "@/utils/index"
export default {

    data() {
        return {
            loginInfo: {
                account: '',
                password: ''
            },
            flag: '0',//登录
        };
    },

    mounted() {

    },

    methods: {
        handelC() {
            this.$router.push('/receptionLogin')
        },
        hanldeFlag() {
            if (this.flag == '0') {
                this.flag = '1'
            } else {
                this.flag = '0'
            }
            console.log(this.flag);
            this.loginInfo.account = ''
            this.loginInfo.password = ''

        },
        async handleRegister() {
            let data = {
                ...this.loginInfo
            }
            if (this.loginInfo.account == '' || this.loginInfo.password == '') {
                this.$message.error('账号或密码不能为空')
            }
            console.log('rr', data);
            const res = await register(data)
            if (res.code == 200) {
                this.$message.success('注册成功')
                this.flag = '0'
                this.loginInfo.account = ''
                this.loginInfo.password = ''
            } else {
                this.$message.error(res.message)
            }
        },
        async handleLogin() {

            let data = {
                password: this.loginInfo.password,
                voucher: this.loginInfo.account
            }
            if (this.loginInfo.account == '' || this.loginInfo.password == '') {
                this.$message.error('账号或密码不能为空')
            }

            const res = await login(data)

            if (res.code == 200) {
                const { token } = res.data
                utils.setCache("token", token)
                const userInfo = {
                    realName:res.data.businessName,
                    photo:res.data.businessImgUrl
                }
                utils.setCache('userInfo',userInfo)
                this.$router.push({
                    path: '/foodManage'
                })
            } else {
                console.log(res);

                this.$message.error(res.message)
            }


        }


    },
};
</script>
<style lang="less" scoped>
.login-wrapper {
    width: 100%;
    height: 100%;
    background-image: url("@/assets/img/login.webp");
    background-size: 100% 100%;
    // background-size: contain;
    background-repeat: no-repeat;
    display: flex;
    align-items: center;
    position: relative;
    background-color: #f2f2f2;

    .login-content {
        width: 400px;
        height: 400px;
        position: absolute;
        right: 250px;
        background-color: #fff;
        border-radius: 15px;
        padding: 30px;
        display: flex;
        flex-direction: column;

        .titleHead {
            display: flex;
            justify-content: center;
            font-size: 20px;
            font-weight: 600;
            margin-bottom: 15px;
        }

        .title {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;

            .text {
                font-size: 22px;
                font-weight: 700;
            }

            .go-register {
                font-size: 12px;

                span {
                    font-size: normal;
                    color: #155bd4;
                    cursor: pointer;
                }
            }
        }

        .content {
            flex: 1;


            ::v-deep {
                .el-input__inner {
                    border: none;
                    background-color: #fff;
                    padding-bottom: 2px;
                    border-bottom: 1px solid #f2f2f2;
                    color: #333;
                }
            }

            .account {}

            .loginBtn {
                margin-top: 50px;
                height: 50px;
                background-color: #155bd4;
                line-height: 50px;
                text-align: center;
                border-radius: 15px;
                color: #fff;
                cursor: pointer;
            }
        }
    }
}
</style>